export default function routerMenu(list) {
  // 菜单
  const menuList = recursion(list);
  const routerList = recursionRouter(list);

  // 处理菜单 递归处理带有下级的数据
  function recursion(list) {
    const newList = [];
    list.forEach((item) => {
      if (item.childrenMenus && item.childrenMenus.length > 0) {
        const children = recursion(item.childrenMenus);
        newList.push({
          name: item.name,
          path: item.routerPath,
          icon: item.iconCls,
          children,
        });
      } else {
        newList.push({
          name: item.name,
          path: item.routerPath,
          icon: item.iconCls,
        });
      }
    });
    return newList;
  }

  // 将所有带有children的数据都处理成同级
  function recursionRouter(list) {
    let result = [];
    for (const item of list) {
      // 带有下级的 不添加到路由数组
      if (item.childrenMenus.length==0) {
        result.push(item.routerPath);
      }
      if (item.childrenMenus && item.childrenMenus.length > 0) {
        const flattenedChildren = recursionRouter(item.childrenMenus);
        result = result.concat(flattenedChildren);
      }
    }
    return result;
  }
  return {
    menuList,
    routerList,
  };
}
